package cn.frank.service.oracle.impl;

import cn.frank.constant.ColorsList;
import cn.frank.constant.DzwzConstants;
import cn.frank.vo.TotalEntry;
import cn.frank.domain.oracle.TJ_JL_FHBD_RI;
import cn.frank.domain.oracle.UCML_Organize;
import cn.frank.mapper.oracle.TJ_JL_FHBD_RIMapper;
import cn.frank.mapper.oracle.UCML_OrganizeMapper;
import cn.frank.service.oracle.ITJ_JL_FHBD_RIService;
import cn.frank.vo.LineShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Frank
 * @date 06-04
 */
@Service
public class ITJ_JL_FHBD_RIServiceImpl implements ITJ_JL_FHBD_RIService {
    @Autowired
    private TJ_JL_FHBD_RIMapper fhbdRiMapper;
    @Autowired
    private UCML_OrganizeMapper organizeMapper;
    @Autowired
    private ColorsList colorsList;
    @Override
    public List<TotalEntry> findRiDayOfTime() {
        List<TotalEntry> list = new ArrayList<>();
        List<TJ_JL_FHBD_RI> riList = fhbdRiMapper.getYwbdDay();
        TotalEntry<String,Integer> e1 = new TotalEntry(DzwzConstants.SALE_DC_JING_CHE,0);
        TotalEntry<String, BigDecimal> e2 = new TotalEntry(DzwzConstants.SALE_DC_JING_DUN,BigDecimal.ZERO);
        TotalEntry<String,Integer> e3 = new TotalEntry(DzwzConstants.SALE_DC_ZHON_CHE,0);
        TotalEntry<String,BigDecimal> e4 = new TotalEntry(DzwzConstants.SALE_DC_ZHON_DUN,BigDecimal.ZERO);
        TotalEntry<String,Integer> e5 = new TotalEntry(DzwzConstants.SALE_DC_MEIN_CHE,0);
        TotalEntry<String,BigDecimal> e6 = new TotalEntry(DzwzConstants.SALE_DC_MEIN_DUN,BigDecimal.ZERO);
        if(riList.size() > 0) {
            //riList.stream().filter(t -> t.getDybdlx().equals(DzwzConstants.DIAO_RU)).forEach(x ->{
            riList.forEach(x ->{
                if(x.getJcInventoryName().equals(DzwzConstants.JING_MEI)){
                    e1.setValue(e1.getValue() + x.getCs());
                    e2.setValue(e2.getValue().add(x.getJz()));
                }
                if(x.getJcInventoryName().equals(DzwzConstants.ZHON_MEI) ){
                    e3.setValue(e3.getValue()+ x.getCs());
                    e4.setValue(e4.getValue().add(x.getJz()));
                }
                if(x.getJcInventoryName().equals(DzwzConstants.MEI_NI) ){
                    e5.setValue(e5.getValue()+ x.getCs());
                    e6.setValue(e6.getValue().add(x.getJz()));
                }
            });
        }

        list.addAll(Arrays.asList(e1,e2,e3,e4,e5,e6));
        return list;
    }

    @Value("${my.screen.inner-data-show-day}")
    private int day;

    @Transactional
    @Override
    public void executeUpdateData() {
        //先查询业务表中的当日数据，如果没有返回
        List<TJ_JL_FHBD_RI> list =  fhbdRiMapper.getYwbdDay();
        if(list.size() == 0) return;
        //再查询统计表中的当日数据，如果有删除，如果没有批量插入 getTjbdDay
        List<TJ_JL_FHBD_RI> tjbdDay = fhbdRiMapper.getTjbdDay();
        //第一种方式 直接删除所有 然后批量插入
        //if(tjbdDay.size() > 0) dybdRiMapper.removeTjbdByRi();
        ////然后批量插入
        //dybdRiMapper.saveTjbdList(list);
        if(tjbdDay.size() > 0) {
            list.forEach(x -> {
                Optional<TJ_JL_FHBD_RI> first =
                        tjbdDay.stream().filter(t -> t.getDcdwid().equals(x.getDcdwid())
                                && t.getJcKeShangDAOID().equals(x.getJcKeShangDAOID())
                                && t.getCcrq().equals(x.getCcrq())
                                && t.getJcInventoryOID().equals(x.getJcInventoryOID())).findFirst();
                if(first.isPresent()){ //修改
                    fhbdRiMapper.modifyTjbdOne(x);
                }else{ //新增
                    fhbdRiMapper.saveTjbdOne(x);
                }
            });
        }else{
            fhbdRiMapper.saveTjbdList(list);
        }
    }


    @Override
    public List<LineShow> findMonthTjbd() {
        //Map<String,Object> map = new HashMap<>();
        //当前当前日期是这个月的第几天
        //LocalDate currentDate = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");

        //int day = currentDate.getDayOfMonth();
        //int day = 10;
        System.out.println(day);//可配置
        List<LineShow> list = new ArrayList<>();
        List<TJ_JL_FHBD_RI> riList = fhbdRiMapper.getTjbdMonth(day-1);
        System.out.println(colorsList.getInnerShowColors());
        //查所有的区域公司
        List<UCML_Organize> regionOrganizes = organizeMapper.getRegionOrganize();

        regionOrganizes.forEach(o -> {
            Calendar calendar = Calendar.getInstance(); // 获取当前日期
            LineShow inner = new LineShow();
            inner.setCategorys(o.getUCML_OrganizeOID());
            calendar.add(Calendar.DAY_OF_YEAR,-day);
            for(int i = 0; i < day; i++){
                calendar.add(Calendar.DATE, 1); // 日期
                inner.getXdata().add(sdf.format(calendar.getTime()));
            }
            list.add(inner);
        });
        if(riList.size() > 0){
            Map<String, List<TJ_JL_FHBD_RI>> map =
                    riList.stream().collect(Collectors.groupingBy(TJ_JL_FHBD_RI::getDcdwParentId));
            map.forEach((x, y) -> {
                //InnerShow innerShow =  list.stream().filter(t -> t.getCategorys().equals(x)).findFirst().get();
                LineShow lineShow =  list.stream().filter(t -> t.getCategorys().equals(x)).findFirst().get();
                int colorIndex = 0;
                for(int i = 0; i < y.size(); i++){
                    TJ_JL_FHBD_RI ri = y.get(i);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(ri.getCcrq());
                    String d = sdf.format(calendar.getTime());
                    String name = ri.getDcdwidName()+ri.getJcInventoryName();
                    int index = lineShow.getNames().indexOf(name);
                    int idx = lineShow.getXdata().indexOf(d);
                    if(index != -1){
                        lineShow.getResult().get(index)[idx] = ri.getJz();
                    }else{
                        lineShow.getNames().add(name);
                        lineShow.getColors().add(colorsList.getInnerShowColors().get(colorIndex++));
                        System.out.println(d);
                        BigDecimal[] bd = new BigDecimal[day];
                        Arrays.fill(bd,BigDecimal.ZERO);
                        bd[idx] = ri.getJz();
                        lineShow.getResult().add(bd);
                    }
                }
                //list.add(innerShow);
                //map.put(x,innerShow);
            });
        }
        //list.sort((a,b)->Collator.getInstance(Locale.CHINA).compare(a.getCategorys(),b.getCategorys()));
        //map.put("data",list);
        return list;
    }
}
